/*
 * TemperaturePlot.cpp
 *
 * Copyright 2012 Pieter Agten
 *
 * This file is part of tltgen.
 *
 * tltgen is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * tltgen is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with tltgen.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @file   TemperaturePlot.cpp
 * @author Pieter Agten (pieter.agten@gmail.com)
 * @date   25 jan 2013
 */

#include <stdexcept>
#include <boost/shared_ptr.hpp>

#include "TemperatureScale.hpp"

#include "TemperaturePlot.hpp"

using boost::shared_ptr;

namespace tltgen {

  TemperaturePlot::TemperaturePlot(const shared_ptr<ThermistorTable>& thermistorTable, const shared_ptr<LeastSquaresSolver>& solver, const shared_ptr<TemperatureScale const>& temperatureScale) :
    ThermistorTablePlot(thermistorTable, solver),
    temperatureScale(temperatureScale)
  {
    if (! isValidTemperatureScale(temperatureScale)) {
      throw std::invalid_argument("The given temperature scale is invalid for this plot.");
    }
  }

  TemperaturePlot::~TemperaturePlot()
  {  }

  bool TemperaturePlot::isValidTemperatureScale(const shared_ptr<TemperatureScale const>& temperatureScale)
  {
    return temperatureScale.get() != NULL;
  }

  void TemperaturePlot::setTemperatureScale(const shared_ptr<TemperatureScale const>& temperatureScale)
  {
    if (! isValidTemperatureScale(temperatureScale)) {
      throw std::invalid_argument("The given temperature scale is invalid for this plot.");
    }

    PLFLT xminKelvin = this->temperatureScale->toKelvin(this->xmin);
    PLFLT xmaxKelvin = this->temperatureScale->toKelvin(this->xmax);
    this->xmin = temperatureScale->fromKelvin(xminKelvin);
    this->xmax = temperatureScale->fromKelvin(xmaxKelvin);
    this->temperatureScale = temperatureScale;
    thermistorTableUpdated();
  }

  shared_ptr<TemperatureScale const> TemperaturePlot::getTemperatureScale() const
  {
    return this->temperatureScale;
  }
    
  unsigned int TemperaturePlot::getXMaxDigits() const
  {
    return 4;
  }  
 
  std::string TemperaturePlot::getXLabel() const
  {
    return "Temperature (" + getTemperatureScale()->getShortUnitName() + ")";
  }

  tuple<PLFLT, PLFLT> TemperaturePlot::getXBoundsAuto() const
  {
    tuple<Temperature, Temperature> tBounds = this->thermistorTable->getTemperatureBounds();
    return tuple<PLFLT, PLFLT>(this->temperatureScale->fromKelvin(tBounds.get<0>()), this->temperatureScale->fromKelvin(tBounds.get<1>()));
  }

  PLFLT TemperaturePlot::getXValue(ThermistorTableRow* thermistorTableRow) const
  {
    return this->temperatureScale->fromKelvin(thermistorTableRow->getTemperature());
  }

}

